let fetch = require("../../utils/fetch");
let mta = require('../../utils/mta_analysis.js');

let app = getApp();
Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		orderInfo: {},
		// goodsList: [],
		imageUrl: getApp().globalData.imageUrl,
		leftStyle: '137.5rpx',
		showFloat: false, //是否显示背景
		showPayPick: false, //是否显示支付渠道
		showOrdermsg: false, //是否显示订单详情
		balance: 0,
		goodsPosition: '',
		class1: 'menu_select',
		class2: 'menu_unselect',
		chooseAddress: '请选择地址', //配送地址
		peisongTime: '请选择', //配送时间
		chooseTimes: null, //选择配送时间
		chooseNote: null, //备注
		isShow: false, //密码输入框
		input_type: true, //免密自动聚焦
		balanceP: false, //是否显示密码框
		orderCode: '', //预订单号
		timeNote: '', //选择事件提示
		orderMsgaddress: '',
		orderMsgTime: '',
		orderMsgaddressNote: '',
		orderMsgTimeNote: '',
		styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']


	},
	hideOrder: function() {
		this.setData({
			showFloat: false,
			showOrdermsg: false
		})

	},
	onLoad: function(options) {

		console.log(options.code)
		this.getOrderInfo(options.code)
		this.getBalance();
	},
	getOrderInfo: function(code) {

		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/order/getOrderByOrderCode.do',
			data: {
				orderCode: code
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading()

				_self.setData({
					orderInfo: res.data.obj
				})
				console.log(res)

				if (res.data.obj.reserveOrderVo.deliveryModel == 10) {
					_self.setData({
						orderMsgaddress: '自提地址',
						orderMsgTime: '自提时间',
						orderMsgaddressNote: res.data.obj.reserveOrderVo.fixedAddress,
						orderMsgTimeNote: res.data.obj.reserveOrderVo.pickupTime,
					})

				} else {
					_self.setData({
						orderMsgaddress: '配送地址',
						orderMsgTime: '配送时间',
						orderMsgaddressNote: res.data.obj.reserveOrderVo.fixedAddress + res.data.obj.reserveOrderVo.deliveryAddress,
						orderMsgTimeNote: res.data.obj.reserveOrderVo.pickupTime,
					})

				}

			}
		})

	},
	onReady: function() {
		console.log('onReady')
	},
	onShow: function() {
		this.canPay = true;
		console.log('选择')
		var badd = getApp().globalData.common.reservebAdd || ''
		var sadd = getApp().globalData.common.reservesAdd || ''
		this.setData({
			chooseAddress: badd + sadd,
			chooseNote: getApp().globalData.common.reservesNote
		})

		this.getqianyueType();



	},
	onHide: function() {},
	onUnload: function() {

	},
	payPickerAction: function() {
		this.setData({
			showPayPick: true,
			showFloat: true
		})
	},
	bindChange: function(e) {
		console.log(e.detail.value)
		this.setData({
			chooseTime: e.detail.value
		})

	},
	cancelTimeAction: function() {
		this.setData({
			showTimePick: false,
			showFloat: false
		})
	},
	cancelPayAction: function() {
		this.setData({
			showFloat: false,
			showPayPick: false
		})
	},
	addressPickerAction: function() {
		console.log('123')
	},

	getBalance: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/wxappletMember/getBalance.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					if (parseFloat(res.data.obj.balance) < parseFloat(_self.data.orderInfo.price)) {
						_self.setData({
							payText: "微信",
							balance: res.data.obj.balance,
							payType: 1
						})

					} else {
						_self.setData({
							payText: "余额:" + res.data.obj.balance,
							balance: res.data.obj.balance,
							payType: 4
						})
					}
				}
				console.log(res)
			}
		})
	},
	payAction: function() {



		this.setData({
			showFloat: true,
			showOrdermsg: true,

		})

	},
	payMakersure: function() {
		if (!this.canPay) {
			return
		}
		this.canPay = false;
		if (this.data.payType == '4' && this.data.balanceP) {
			this.setData({
				isShow: true,
				styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style'],
				showOrdermsg: false

			})
			return
		} else {
			this.pay(this.data.orderInfo.orderCode, this.data.payType);
		}

		this.setData({
			showFloat: false,
			showOrdermsg: false
		})



	},
	pay: function(code, type) {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/pay/v1/pay.do?orderCode=' + code + "&payType=" + type,
			method: 'post',
			header: getApp().globalData.header,
			success: function(res) {


				wx.hideLoading();
				if (res.data.code == 200) {
					if (type == "4") {
						wx.redirectTo({
							url: `../fulfil/fulfil?type=1&orderCode=${res.data.obj.orderCode}&discountPrice=${res.data.obj.discountPrice}&orderPrice=${res.data.obj.orderPrice}`
						})
						return
					}
					if (res.data.obj.status == "2") {
						wx.redirectTo({
							url: '../orders/orders?current=0'
						})
						return
					}
					if (res.data.obj.type == '0') {
						console.log("非免密支付")
						wx.requestPayment({
							'timeStamp': res.data.obj.timeStamp,
							'nonceStr': res.data.obj.nonceStr,
							'package': res.data.obj.prepay_id,
							'signType': res.data.obj.signType,
							'paySign': res.data.obj.paySign,
							'success': function(response) {

								// wx.switchTab({     url:'../mine/mine' })
								wx.redirectTo({
									url: `../fulfil/fulfil?type=1&orderCode=${res.data.obj.orderCode}&discountPrice=${res.data.obj.discountPrice}&orderPrice=${res.data.obj.orderPrice}`
								})
							},
							'fail': function(res) {

								wx.redirectTo({
									url: '../orders/orders?current=0'
								})
							}
						}) // 支付方式为余额支付时

					} else if (res.data.obj.type == '1') {
						console.log("免密支付")
						wx.redirectTo({
							url: `../fulfil/fulfil?type=1&orderCode=${res.data.obj.orderCode}&discountPrice=${res.data.obj.discountPrice}&orderPrice=${res.data.obj.orderPrice}`
						})
					}

				} else {
					console.log("支付失败")
					wx.redirectTo({
						url: '../orders/orders?current=0'
					})
				}
			}

		})


	},
	choosePatType: function(e) {
		// return
		console.log(e.currentTarget.dataset.code);
		var code = e.currentTarget.dataset.code

		if (code == '0') {
			if (this.data.balance < this.data.shopMarket.promotionPrice) {
				this.setData({
					payType: "11",
					payText: "微信"
				})


			} else {
				this.setData({
					payType: "4",
					payText: "余额:" + this.data.balance,
				})

			}
		} else if (code == '1') {

			this.setData({
				payType: "11",
				payText: "微信"
			})


		}
		this.cancelPayAction();

	},
	showMyToast: function(self, my_toast_tip, time = 1000, template = 'my_toast') {
		self.setData({
			template: template, //my_toast
			my_toast_tip: my_toast_tip
		})
		var timer = setTimeout(() => {
			self.setData({
				template: '', //my_toast
				my_toast_tip: ''
			})
			clearTimeout(timer);
		}, time)

	},
	maixiangTurn: function() {
		var mxUrl = '../maixiang/maixiang';

		getApp().globalData.common.mxShopCode = getApp().globalData.common.reserveShopCode;
		wx.navigateTo({
			url: mxUrl
		})

	},

	//获取签约状态
	getqianyueType: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/freepay/v3/sign/state.do',
			method: 'get',
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				if (res.data.obj.BALANCE == 0) {
					_self.setData({
						balanceP: true
					})
				} else {
					_self.setData({
						balanceP: false
					})
				}


			}

		})
	},
	//密码
	getPassWord: function(e) {
		var _self = this;
		console.log(e.detail.value.length)

		var arr = _self.data.styleList
		for (var i = 0; i < 6; i++) {
			if (i <= e.detail.value.length - 1) {

				arr[i] = 'select_style'
			} else {
				arr[i] = 'noSelect_style'
			}
		}
		if (e.detail.value.length == 6) {
			_self.filterPassword(e.detail.value);
		}
		_self.setData({
			styleList: arr
		})
		console.log(_self.data.styleList)


	},
	hideView: function() {
		this.setData({
			input_type: true
		})

	},
	closeView: function() {
		this.canPay = true;
		this.setData({
			showFloat: false,
			isShow: false,
			balanceStatus: false,
			styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']

		})
	},
	filterPassword: function(num) {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/balance/pwd/check.do?password=' + num,
			method: 'post',
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					console.log("调用支付");
					_self.pay(_self.data.orderInfo.orderCode, 4)
					_self.setData({
						balanceStatus: true
					})
				} else {
					_self.setData({
						balanceStatus: false,
						isShow: false,
						styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']
					})
					_self.canPay = true;
					fetch.showMyToast(_self, "密码错误")
				}

			}

		})

	}


})